package com.api.weavernorth.OcrCase;

import com.alibaba.fastjson.JSONObject;
import com.cloudstore.eccom.pc.table.WeaTable;
import com.cloudstore.eccom.pc.table.WeaTableColumn;
import com.cloudstore.eccom.pc.table.WeaTableOperate;
import com.cloudstore.eccom.pc.table.WeaTableOperates;
import com.weavernorth.util.Logger4jUtil;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.PageIdConst;

import org.dom4j.DocumentException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: BruseeWang
 * @Date: 2021/4/26 18:05
 * @Description: 处理配置页面的信息，将配置页面传递过来的信息存储到自行创建的表结构中
 */
@Path("/weavernorth/OcrCase/AllocationPage")
public class AllocationPage extends HttpServlet {
    Logger log = Logger4jUtil.getLoggerByName("/action/api/OcrCase/AllocationPage/");

    /****
     * 将前台传过来的信息存储到后台表中，并返回是否成功的信息以及前台传过来的信息是否是空
     * @param request:接口请求
     * @param response:接口响应
     * @return 配置页面的更新信息成功与否
     * @throws IOException
     * @throws DocumentException
     */
    @GET
    @Path("/setAllocationPage")
    @Produces(MediaType.TEXT_PLAIN)
    public String setAllocationPage(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException, DocumentException {
        // 路径类型
        String strWorkFlowId = request.getParameter("wrokflowid");
        log.info("strWorkFlowId--" + strWorkFlowId);
        // 供应商类型
        String strSupplierType = request.getParameter("SupplierType");
        log.info("strSupplierType--" + strSupplierType);
        // 识别类型
        String strOCRType = request.getParameter("OCRType");
        log.info("strOCRType--" + strOCRType);
        // Appkey
        String strAppkey = request.getParameter("Appkey");
        log.info("strAppkey--" + strAppkey);
        // PassWord
        String strPassWord = request.getParameter("PassWord");
        log.info("strPassWord--" + strPassWord);
        // 数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        // 所有前台传过来的数据都不允许为空
        if ("".equals(strWorkFlowId)  || "".equals(strSupplierType)  || "".equals(strOCRType) || "".equals(strAppkey) || "".equals(strPassWord) ) {
            // 只要有一个数据为空就要提示
            return "必填配置项为空，请重新填写，所有配置项均为必填项";
        } else {
            String strWorkFlowName = "";
            // 根据workflowid获取路径的显示名
            String strSql2 = "select workflowname from workflow_base where id = '" +strWorkFlowId+ "'";
            rs2.execute(strSql2);
            if(rs2.next()){
                strWorkFlowName = rs2.getString("workflowname");
            }
            // 根据workflowid 与 供应商类型查询数据库中是否有数据
            String strSql = "select * from uf_OCR_Page where workflowid = '" + strWorkFlowId + "' and SupplierType = '" + strSupplierType + "'";
            log.info("查询是否有配置项的sql--" + strSql);
            rs.execute(strSql);
            if (rs.next()) {
                // 若有数据判断识别类型是否有如果有进行提示配置重复
                if (strOCRType.equals(rs.getString("OcrType"))) {
                    // 进行数据更新
                    String strSql1 = "update uf_OCR_Page set SupplierType = '"+strSupplierType+"',OcrType = '"+strOCRType
                            +"',AppKey = '"+strAppkey+"',PassWord = '"+strPassWord+"',workflowname = '"+strWorkFlowName+"' where workFlowId = '"+strWorkFlowId+"'";
                    rs1.execute(strSql1);
                    return "更新成功";
                }else{
                    try {
                        // 将穿过来的数据存储到数据库中
                        String strSql1 = "insert into uf_OCR_Page(workFlowId,SupplierType,OcrType,AppKey,PassWord,workflowname) values('" + strWorkFlowId + "','" + strSupplierType + "','" + strOCRType + "','" + strAppkey + "','" + strPassWord + "','"+strWorkFlowName+"')";
                        log.info("进行存储数据的sql---" + strSql1);
                        rs1.execute(strSql1);
                        return "配置成功";
                    } catch (Exception e) {
                        log.info("e--" + e.toString());
                        return "未知异常，请联系管理员";
                    }
                }
            } else {
                try {
                    // 将传过来的数据存储到数据库中
                    String strSql1 = "insert into uf_OCR_Page(workFlowId,SupplierType,OcrType,AppKey,PassWord,workflowname) values('" + strWorkFlowId + "','" + strSupplierType + "','" + strOCRType + "','" + strAppkey + "','" + strPassWord + "','"+strWorkFlowName+"')";
                    log.info("进行存储数据的sql---" + strSql1);
                    rs1.execute(strSql1);
                    return "配置成功";
                } catch (Exception e) {
                    log.info("e--" + e.toString());
                    return "未知异常，请联系管理员";
                }
            }
        }
    }

    /****
     *  根据前台选中的字段将字段的 fieldId 存入身份证识别表中
     * @param request
     * @param response
     * @return String 是否配置成功
     */
    @GET
    @Path("/setFielId")
    @Produces(MediaType.TEXT_PLAIN)
    public String setFielId(@Context HttpServletRequest request, @Context HttpServletResponse response){
        // 姓名
        String strName = request.getParameter("name");
        // 身份证号
        String strNumber =  request.getParameter("number");
        // 性别
        String strSex = request.getParameter("sex");
        // 民族
        String strNation = request.getParameter("nation");
        // 出生年月
        String strBirth = request.getParameter("birth");
        // 家庭住址
        String strAddress = request.getParameter("address");
        // 发证机关
        String strOffice = request.getParameter("office");
        // 证件开始日期
        String strBegin = request.getParameter("begin");
        // 证件结束日期
        String strEnd = request.getParameter("end");
        // 流程id
        String strWorkFlowId = request.getParameter("workflowid");

        log.info("strName---" + strName+ "---strNumber---" +strNumber+ "---strSex---"+strSex+"---strNation---" +strNation+ "---strBirth---" +strBirth+
                "---strAddress---" +strAddress+ "---strOffice---" +strOffice+ "---strOffice---" +strOffice+
                "---strBegin---" +strBegin+ "---strEnd---" +strEnd+ "---strWorkFlowId---" +strWorkFlowId );

        // 数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        // 首先判断表中是否已经配这条流程的一个数据
        String strSql = "select * from uf_OCR_CardFieldPag where workFlowId = '"+strWorkFlowId+"'";
        rs.execute(strSql);
        if(rs.next()){
            // 有数据进行更新
            String strSlq1 = "update uf_OCR_CardFieldPag set name = '"+strName+"',num = '"+strNumber+"',sex = '"+strSex+"'" +
                    ",nation = '"+strNation+"', birth = '"+strBirth+"', address = '"+strAddress+"',office = '"+strOffice+"'" +
                    ",beginTime = '"+strBegin+"',endTime = '"+strEnd+"' where workFlowId = '"+strWorkFlowId+"'";
            log.info("进行更新数据的sql---" + strSlq1);
            rs1.execute(strSlq1);
            return "更新配置成功";
        }else{
            // 没有数据进行插入
            String strSlq1 = "insert into uf_OCR_CardFieldPag(name,num,sex,nation,birth,address,office,beginTime,endTime,workFlowId)" +
                    " values('"+strName+"','"+strNumber+"','"+strSex+"','"+strNation+"','"+strBirth+"','"+strAddress+"'" +
                    ",'"+strOffice+"','"+strBegin+"','"+strEnd+"','"+strWorkFlowId+"')";
            log.info("进行插入的sql---" + strSlq1);
            rs1.execute(strSlq1);
            return "配置成功";
        }
    }

    /****
     * 分页列表用于展示配置页面所配置的所有数据
     * @param request
     * @param response
     * @return json数据
     */
    @GET
    @Path("/ocrTable")
    @Produces(MediaType.TEXT_PLAIN)
    public String ocrTable(@Context HttpServletRequest request, @Context HttpServletResponse response){
        JSONObject jsonObject = new JSONObject();
        String strPageid = "OcrViewTable";
        String strPageSize = PageIdConst.getPageSize(strPageid, 1, strPageid);
        // WeaTable用于给前端table组件组装数据的辅助类
        WeaTable table = new WeaTable();
        //PageSize保证唯一
        table.setPageID(strPageid);
        table.setPagesize(strPageSize);

        //sql的设置  select id,lastname,sex from hrmresource
        //添加查询字段
        table.setBackfields("id,workflowname,SupplierType,OcrType,AppKey");
        //添加查询数据库表
        table.setSqlform(" uf_OCR_Page ");
        //添加查询条件
        table.setSqlwhere("");
        //添加根据哪个字段排序
        table.setSqlorderby("ID");
        //添加主键
        table.setSqlprimarykey("ID");
        //设置是否去重
        table.setSqlisdistinct("false");
        //添加排序方式
        table.setSqlsortway("desc");
        //设置列属性
        table.getColumns().add(new WeaTableColumn("5%", "id", "id","","2"));
        table.getColumns().add(new WeaTableColumn("20%", "路径类型", "workflowname"));
        table.getColumns().add(new WeaTableColumn("25%", "供应商类型", "SupplierType"));
        table.getColumns().add(new WeaTableColumn("25%", "识别类型", "OcrType"));
        table.getColumns().add(new WeaTableColumn("25%", "APPkey", "AppKey"));
        WeaTableOperates weaTableOperates = new WeaTableOperates();

        List<WeaTableOperate> operateList = new ArrayList<>();
        log.info("operateList" + operateList.toString());
        WeaTableOperate classpathoperate = new WeaTableOperate("查看配置页面", "", "0");
        // 添加复选框控制

        operateList.add(classpathoperate);
        weaTableOperates.setOperate(operateList);
        table.setOperates(weaTableOperates);
        Map map = table.makeDataResult();
        log.info("datas" +  map.get("datas"));
        map.put("sessionkey", map.get("datas"));
        jsonObject.putAll(map);
        return jsonObject.toString();
    }

    /***
     * 进项删除信息
     * @param request 传过来的keys
     * @param response
     * @return 返回成功失败状态
     */
    @GET
    @Path("/DeleteWorkFlowid")
    @Produces(MediaType.TEXT_PLAIN)
    public String DeleteWorkFlowid(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        // 获取传过来的keys
        String strKeys = request.getParameter("RowKeys");
        log.info("strKeys---" +strKeys);
        RecordSet rs = new RecordSet();
        String strSql = "delete from uf_OCR_Page where id in("+strKeys+")";
        log.info("删除信息的sql--- " + strSql);
        rs.execute(strSql);
        Map<String,String> map = new HashMap<>();
        map.put("flag","true");
        map.put("message","删除成功");
        return JSONObject.toJSONString(map);
    }

    /***
     * 根据传过来的流程路径明获取流程路径Id
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getWorkFlowid")
    @Produces(MediaType.TEXT_PLAIN)
    public String getWorkFlowid(@Context HttpServletRequest request, @Context HttpServletResponse response){
        RecordSet rs = new RecordSet();
        // 流程路径名
        String strWorkFlowName = request.getParameter("workflowname");
        log.info("获取到的strWorkFlowName---" + strWorkFlowName);
        String strWorkFlowId = "";
        // 根据workflowid获取路径的显示名
        String strSql2 = "select id from workflow_base where workflowname = '" +strWorkFlowName+ "'";
        rs.execute(strSql2);
        if(rs.next()){
            strWorkFlowId = rs.getString("id");
        }
        return strWorkFlowId;
    }
}
